//显示正确的选项,在提交后
function showCorrectAnswer(flag) {
    showCorrectAnswerWordSelect(flag)
    showCorrectAnswerGrammarSelect(flag)
    showCorrectAnswerGrammarWord(flag)
    showCorrectAnswerWordListened(flag)
    showCorrectAnswerSentenceListened(flag)
    showCorrectAnswerParagraphListened(flag)
    showCorrectAnswerReadingSelect(flag)
    showCorrectAnswerReadingInput(flag)
}

//1.第一题-单词选择
function showCorrectAnswerWordSelect(flag) {
    const inner = document.querySelectorAll('.cardSelectDivOptions')
    inner.forEach((option, index) => {
        // console.log(index +'---'+ option.tagName.toLowerCase());
        let childrenByLevel = getChildrenByLevel(option, 1);
        childrenByLevel.forEach((optionChild, index) => {
            let userSelectOption = optionChild.dataset.userSelectOption;
            const optionText = optionChild.firstElementChild.firstElementChild.nextElementSibling.textContent
            if (userSelectOption === optionText) {
                // console.log(userSelectOption);
                if (userSelectOption === optionChild.dataset.correct) {
                    optionChild.classList.remove('option-selected');
                    optionChild.classList.add('border-green-500', 'bg-green-50');
                    // 添加正确标记
                    const correctIcon = document.createElement('i');
                    correctIcon.className = 'fa fa-check absolute right-4 top-1/2 transform -translate-y-1/2 text-green-500';
                    optionChild.appendChild(correctIcon);
                } else if (userSelectOption !== undefined) {
                    if (flag) {
                        optionChild.classList.remove('option-selected');
                        optionChild.classList.add('border-red-500', 'bg-red-50');
                        // 添加错误标记
                        const wrongIcon = document.createElement('i');
                        wrongIcon.className = 'fa fa-times absolute right-4 top-1/2 transform -translate-y-1/2 text-red-500';
                        optionChild.appendChild(wrongIcon);
                    }
                }
            }
        })
    })
}

//2-第二题-语法选择-句子
function showCorrectAnswerGrammarSelect(flag) {
    let element = document.querySelectorAll('input[name^="select"]:checked');
    element.forEach((option, index) => {
        const optionResult = grammarSentenceCorrect[index] === parseInt(option.value.toString());
        // console.log(option);
        const parentByLevel = getParentByLevel(option, 3);
        if (optionResult) {
            option.parentElement.classList.add('border-green-500', 'bg-green-50');
            // 添加正确标记
            const correctIcon = document.createElement('i');
            correctIcon.className = 'fa fa-check absolute right-4 top-1/2 transform -translate-y-1/2 text-green-500';
            parentByLevel.appendChild(correctIcon);
        } else {
            if (flag) {
                // 添加错误标记
                const wrongIcon = document.createElement('i');
                wrongIcon.className = 'fa fa-times absolute right-4 top-1/2 transform -translate-y-1/2 text-red-500';
                parentByLevel.appendChild(wrongIcon);
            }
        }
    })
}

//2-第二题-语法选择-单词
function showCorrectAnswerGrammarWord(flag) {
    let element = document.querySelectorAll('input[name^="grammar"]:checked');
    element.forEach((option, index) => {
        const optionResult = grammarWordCorrect[index] === parseInt(option.value.toString());
        // console.log(option);
        const parentByLevel = getParentByLevel(option, 2);
        // console.log(parentByLevel);
        if (optionResult) {
            option.parentElement.classList.add('border-green-500', 'bg-green-50');
            // 添加正确标记
            const correctIcon = document.createElement('i');
            correctIcon.className = 'fa fa-check absolute right-4 top-1/2 transform -translate-y-1/2 text-green-500';
            parentByLevel.appendChild(correctIcon);
        } else {
            if (flag) {
                // 添加错误标记
                const wrongIcon = document.createElement('i');
                wrongIcon.className = 'fa fa-times absolute right-4 top-1/2 transform -translate-y-1/2 text-red-500';
                parentByLevel.appendChild(wrongIcon);
            }
        }
    })
}

//3-第四题-听力单词
function showCorrectAnswerWordListened(flag) {
    let elements = document.getElementById('wordListenDataContainer').children

    // console.log(elements);
    const elementArray = [...elements];
    elementArray.forEach((option, index) => {
        const inputElement = option.getElementsByTagName('input');
        const resultFlag = listenWordShuffle[index] === inputElement[0].value;
        if (resultFlag) {
            // 添加正确标记
            const correctIcon = document.createElement('i');
            correctIcon.className = 'fa fa-check absolute right-4 top-1/2 transform -translate-y-1/2 text-green-500';
            option.appendChild(correctIcon);
        } else {
            if (flag) {
                // 添加错误标记
                const wrongIcon = document.createElement('i');
                wrongIcon.className = 'fa fa-times absolute right-4 top-1/2 transform -translate-y-1/2 text-red-500';
                option.appendChild(wrongIcon);
            }
        }
    })
}

//4-第五题-听力短文
function showCorrectAnswerParagraphListened(flag) {
    let elements = document.getElementById('inputContainer').children

    const elementArray = [...elements];
    elementArray.forEach((option, index) => {
        const inputElement = option.getElementsByTagName('input');
        // console.log(option)
        const resultFlag = listenParagraphContext[index] === inputElement[0].value;
        // console.log(option.lastElementChild);
        if (resultFlag) {
            // 添加正确标记
            const correctIcon = document.createElement('i');
            correctIcon.className = 'fa fa-check absolute right-4 top-1/2 transform -translate-y-1/2 text-green-500';
            option.appendChild(correctIcon);
        } else {
            if (flag) {
                // 添加错误标记
                const wrongIcon = document.createElement('i');
                wrongIcon.className = 'fa fa-times absolute right-4 top-1/2 transform -translate-y-1/2 text-red-500';
                option.appendChild(wrongIcon);
            }
        }
    })
}

//4-听力选择-单词
function showCorrectAnswerSentenceListened(flag) {
    let element = document.querySelectorAll('input[name^="listen_select"]:checked');
    element.forEach((option, index) => {
        const optionResult = listenSelectContext[index] === parseInt(option.value.toString());
        // console.log(option);
        const parentByLevel = getParentByLevel(option, 3);
        // console.log(parentByLevel);
        if (optionResult) {
            option.parentElement.classList.add('border-green-500', 'bg-green-50');
            // 添加正确标记
            const correctIcon = document.createElement('i');
            correctIcon.className = 'fa fa-check absolute right-4 top-1/2 transform -translate-y-1/2 text-green-500';
            parentByLevel.appendChild(correctIcon);
        } else {
            if (flag) {
                // 添加错误标记
                const wrongIcon = document.createElement('i');
                wrongIcon.className = 'fa fa-times absolute right-4 top-1/2 transform -translate-y-1/2 text-red-500';
                parentByLevel.appendChild(wrongIcon);
            }
        }
    })
}

//5-第六题-阅读理解-选择
function showCorrectAnswerReadingSelect(flag) {
    const elementNodeList = document.querySelectorAll('.select');
    const lists = [...elementNodeList];
    // console.log(lists.length);
    lists.forEach((list, listIndex) => {
        // console.log(list.value);
        if (readingCorrect[listIndex].toString() === list.value) {
            list.parentNode.classList.add('border-green-500', 'bg-green-50');
            // 添加正确标记
            const correctIcon = document.createElement('i');
            correctIcon.className = 'fa fa-check absolute right-8 top-1/4 transform -translate-y-1/2 text-green-500';
            list.parentNode.insertBefore(correctIcon, option.nextSibling);
        } else {
            if (flag) {
                list.parentNode.classList.add('border-red-500', 'bg-red-50');
                // 添加错误标记
                const wrongIcon = document.createElement('i');
                wrongIcon.className = 'fa fa-times absolute right-8 top-1/2 transform -translate-y-1/2 text-red-500';
                list.parentNode.appendChild(wrongIcon);
            }
        }
    })
}

//5-第六题-阅读理解-输入
function showCorrectAnswerReadingInput(flag) {
    const element = document.getElementById('readingInputContainer');
    const inputElements = element.querySelectorAll('input');
    // console.log(inputElement);
    // let element = document.querySelectorAll('input');
    inputElements.forEach((option, index) => {
        const optionResult = listenSelectContext[index + 3] === parseInt(option.value.toString());
        const parentByLevel = getParentByLevel(option, 1);
        // console.log(parentByLevel);
        if (optionResult) {
            option.parentElement.classList.add('border-green-500', 'bg-green-50');
            // 添加正确标记
            const correctIcon = document.createElement('i');
            correctIcon.className = 'fa fa-check absolute right-4 top-1/2 transform -translate-y-1/2 text-green-500';
            parentByLevel.appendChild(correctIcon);
        } else {
            if (flag) {
                // 添加错误标记
                const wrongIcon = document.createElement('i');
                wrongIcon.className = 'fa fa-times absolute right-4 top-1/2 transform -translate-y-1/2 text-red-500';
                parentByLevel.appendChild(wrongIcon);
            }
        }
    })
}
